/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 音乐播放页面测试
 */

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import '../../lib/pages/music_player_page.dart';
import '../../lib/viewmodels/music_player_view_model.dart';
import '../../lib/models/music_item_model.dart';

void main() {
  group('MusicPlayerPage Tests', () {
    late MusicPlayerViewModel mockViewModel;

    setUp(() {
      mockViewModel = MusicPlayerViewModel();
    });

    tearDown(() {
      mockViewModel.dispose();
    });

    testWidgets('应该显示音乐播放页面的基本UI元素', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: ChangeNotifierProvider<MusicPlayerViewModel>.value(
            value: mockViewModel,
            child: const MusicPlayerPage(),
          ),
        ),
      );

      // 验证基本UI元素存在
      expect(find.byType(Scaffold), findsOneWidget);
      expect(find.byIcon(Icons.close_rounded), findsOneWidget);
      expect(find.text('暂无播放内容'), findsOneWidget);
      expect(find.text('请选择音乐开始播放'), findsOneWidget);
    });

    testWidgets('应该显示示例音乐信息', (WidgetTester tester) async {
      // 创建示例音乐
      final sampleMusic = MusicItemModel(
        id: 'test_001',
        title: '测试歌曲',
        artist: '测试艺术家',
        album: '测试专辑',
        audioUrl: 'https://example.com/test.mp3',
        sourceType: 'test',
      );

      await tester.pumpWidget(
        MaterialApp(
          home: ChangeNotifierProvider<MusicPlayerViewModel>.value(
            value: mockViewModel,
            child: MusicPlayerPage(
              music: sampleMusic,
              isCurrentPlaying: true,
              showRealTimeProgress: true,
            ),
          ),
        ),
      );

      // 验证音乐信息显示
      expect(find.text('测试歌曲'), findsOneWidget);
      expect(find.text('测试艺术家'), findsOneWidget);
    });

    testWidgets('应该显示播放控制按钮', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: ChangeNotifierProvider<MusicPlayerViewModel>.value(
            value: mockViewModel,
            child: const MusicPlayerPage(),
          ),
        ),
      );

      // 验证播放控制按钮存在
      expect(find.byIcon(Icons.repeat_rounded), findsOneWidget); // 播放模式按钮
      expect(find.byIcon(Icons.queue_music_rounded), findsOneWidget); // 播放列表按钮

      // 验证主播放按钮存在
      expect(find.byIcon(Icons.play_arrow), findsOneWidget);
    });

    testWidgets('应该响应播放按钮点击', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: ChangeNotifierProvider<MusicPlayerViewModel>.value(
            value: mockViewModel,
            child: const MusicPlayerPage(),
          ),
        ),
      );

      // 点击播放按钮
      await tester.tap(find.byIcon(Icons.play_arrow));
      await tester.pump();

      // 验证没有异常抛出
      expect(tester.takeException(), isNull);
    });

    testWidgets('应该响应播放模式切换', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: ChangeNotifierProvider<MusicPlayerViewModel>.value(
            value: mockViewModel,
            child: const MusicPlayerPage(),
          ),
        ),
      );

      // 点击播放模式按钮
      await tester.tap(find.byIcon(Icons.repeat_rounded));
      await tester.pump();

      // 验证没有异常抛出
      expect(tester.takeException(), isNull);
    });

    testWidgets('应该显示关闭按钮并响应点击', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: ChangeNotifierProvider<MusicPlayerViewModel>.value(
            value: mockViewModel,
            child: const MusicPlayerPage(),
          ),
        ),
      );

      // 验证关闭按钮存在
      expect(find.byIcon(Icons.close_rounded), findsOneWidget);

      // 点击关闭按钮
      await tester.tap(find.byIcon(Icons.close_rounded));
      await tester.pump();

      // 验证没有异常抛出
      expect(tester.takeException(), isNull);
    });
  });
}
